//
//  ChangePwdInputView.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/8/2.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "ChangePwdInputView.h"

@implementation ChangePWDInputView 

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self s_setupViews];
    }
    return  self;
}
- (void)s_setupViews {
    
    self.titleLab = [[RZCustomLable alloc] initWithFrame:CGRectMake(10, 0, 125, 35)];
    self.titleLab.font = CustomFont(13);
    self.titleLab.textColor = [UtilCheck hexStringToColor:@"555555" alpha:1];
    [self addSubview:self.titleLab];
    
    self.errorLab = [[RZCustomLable alloc] initWithFrame:CGRectMake(self.titleLab.right, 0, SCREEN_WIDTH - self.titleLab.width - 20, 35)];
    self.errorLab.textColor = [UIColor redColor];
    self.errorLab.textAlignment = NSTextAlignmentRight;
    self.errorLab.text = [UtilCheck language:@"setting.limitPWD"];
    self.errorLab.font = CustomFont(8);
    self.errorLab.hidden = YES;
    [self addSubview:self.errorLab];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.titleLab.bottom, SCREEN_WIDTH, 45)];
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    
    self.inputText = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, backView.width - 20, backView.height)];
    self.inputText.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.inputText.clearsOnBeginEditing = NO;
    self.inputText.backgroundColor = [UIColor whiteColor];
    self.inputText.placeholder = [UtilCheck language:@"setting.limitPWD"];
    self.inputText.secureTextEntry = YES;
    self.inputText.font = CustomFont(14);
    self.inputText.delegate = self;
    [backView addSubview:_inputText];
    
}


#pragma mark- TextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self validatePassword:textField.text]) {
        
       self.errorLab.hidden = YES;
    }
    else {
        self.errorLab.hidden = NO;
    }
}

/**
 *  检查密码是否合法
 *
 *  @param passWord 密码
 *
 *  @return 合法 YES 不合法 NO
 */
- (BOOL) validatePassword:(NSString *)passWord

{
    NSString *passWordRegex = @"^(?![0-9]{0,24}$)[\\S]{6,24}$";
    
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

@end
